package cn.good.yan.w5.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * 处理整个的异常
 * 统一异常处理注解  - 正常使用 (包来源于 - Springboot的包)
 *      在 （basics）项目中b7 异常这里也有介绍
 * @author shengren.yan
 * @create 2023-10-06
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 获取其它异常。包括500
     */
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR,reason = "用户访问太多")
    @ExceptionHandler(value = Exception.class)
    public Map<String, Object> defaultErrorHandler1(Exception e) {
        log.error("Exception", e);
        // 封装返回结果
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 500);
        map.put("message", e.getMessage());
        return map;
    }

    /**
     * 获取其它异常。包括500
     */
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
//    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR,reason = "用户访问太多")
    @ExceptionHandler(value = Exception.class)
    public Map<String, Object> defaultErrorHandler(Exception e) {
        log.error("Exception", e);
        // 封装返回结果
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 500);
        map.put("message", e.getMessage());
        return map;
    }

    // 只处理MethodArgumentNotValidException异常，验证相关异常  - 效验证注解 @Validated
//    @ExceptionHandler(value = MethodArgumentNotValidException.class)
//    public R<Object> handleValidException(MethodArgumentNotValidException e) {
//        log.error("数据校验出现问题{},异常类型: {}", e.getMessage(), e.getClass());
//        BindingResult bindingResult = e.getBindingResult();
//        Map<String, String> errorMap = new HashMap<>(16);
//        bindingResult.getFieldErrors().forEach((fieldError -> {
//            errorMap.put(fieldError.getField(), fieldError.getDefaultMessage());
//        }));
//        return R.restResult(errorMap, 500, "数据格式错误");
//    }

    // 处理全部的异常 ，全部异常
//    @ExceptionHandler(value = Throwable.class)
//    public R<Object> handleException(Throwable throwable) {
//        log.error("错误:", throwable);
//        return R.fail(500, "服务器错误");
//    }

    /**
     * 400 异常处理 -
     */
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
//    @ExceptionHandler(MissingServletRequestParameterException.class)
//    public R<Object> e1(MissingServletRequestParameterException e) {
//        log.error("缺少请求参数:", e);
//        return R.fail(400, "400异常");
//    }

    /**
     * 405 - Method Not Allowed
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Map<String, Object> e2(HttpRequestMethodNotSupportedException e) {
        log.error("缺少请求参数:", e);
        // 封装返回结果
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 405);
        map.put("message", e.getMessage());
        return map;
    }


}
